<?php

namespace App\Customer\Model;


use App\Service\EsSearch;
use App\Web\Model\CaseFlow;
use App\Web\Model\Manager;
use function GuzzleHttp\Psr7\str;

class Operator extends Model {

    /**
     * @param $date
     * @param $subId
     * @param $hrUid
     * @return array
     */
    public function getMonthTasks($date, $subId, $hrUid) {
        $caseModel = new CaseModel();
        $caseIds = $caseModel->getRedisCaseIds($hrUid, $subId);
        $searchEqual['caseid'] = $caseIds;
        $searchEqual['subid'] = $subId;
        $searchEqual['waitflow'] = 1;
        $searchEqual['status'] = '01';
        $startTime = strtotime($date);
        $firstday =  date('Y-m-01', strtotime($date));
        $endTime = strtotime( date("Y-m-d", strtotime("$firstday +1 month -1 day")) . " 23:59:59" );
        $searchRange['timecontrol'] = [EsSearch::FILE_RANGE_GTE => $startTime, EsSearch::FILE_RANGE_LTE => $endTime];

        //去除隐藏字段
        $unShowFlowatList = (new CaseResumeModel())->getUnShowFloat(1);
        $searchParam = [
            EsSearch::EQUAL => $searchEqual,
            EsSearch::RANGE => $searchRange,
            EsSearch::NOTEQUAL => $unShowFlowatList
        ];
        $esSearch = EsSearch::getInstance();
        $waitFlowData = $esSearch->setIndex(EsSearch::ES_INDEX_CASE_EN_RESUME)->setFields('timecontrol,keycode')->setWhere($searchParam)->search();
        if ($waitFlowData['eserror'] == 1 || empty($waitFlowData['data'])) {
            return [];
        }
        $waitFlowat = [];
        foreach ($waitFlowData['data'] as $data) {
            $day = date('j', $data['timecontrol']);
            if (array_key_exists($day, $waitFlowat)) {
                $waitFlowat[$day] += 1;
            } else {
                $waitFlowat[$day] = 1;
            }
        }
        return $waitFlowat;
    }


    /**
     * 获取详细带跟进数据详细信息
     * @param $subId
     * @param $date
     * @param $hrUid
     * @return array
     * @throws \Exception
     */
    public function getReportInfo($subId, $date, $hrUid) {
        //格式化返回结果
        $reportList = [
            'todaywaitlist' => []
        ];

        $timecontrolBegin = strtotime($date);
        $timecontrolEnd = $timecontrolBegin + 24 * 3600 - 1;
        $caseModel = new CaseModel();
        $caseIds = $caseModel->getRedisCaseIds($hrUid, $subId);
        $searchEqual['caseid'] = $caseIds;
        $searchEqual['status'] = "01";
        $searchEqual['waitflow'] = 1;
        $searchEqual['subid'] = $subId;
        $searchRange['timecontrol'] = [EsSearch::FILE_RANGE_GTE => $timecontrolBegin, EsSearch::FILE_RANGE_LTE => $timecontrolEnd];
        //去除隐藏字段
        $unShowFlowatList = (new CaseResumeModel())->getUnShowFloat(1);
        $searchParam = [
            EsSearch::EQUAL => $searchEqual,
            EsSearch::RANGE => $searchRange,
            EsSearch::NOTEQUAL => $unShowFlowatList
        ];
        $esSearch = EsSearch::getInstance();
        $field = "keycode,caseresumeid,resumeid,projectid,caseid,subid,flowat,flowatstatus,tagadderid,tagadddate,timecontrol,waitflow";
        $caseResumeList = $esSearch->setIndex(EsSearch::ES_INDEX_CASE_EN_RESUME)->setFields($field)->setWhere($searchParam)->search();
        if ($caseResumeList['eserror'] == 1 || empty($caseResumeList['data'])) {
            return $reportList;
        }
        $reportList['todaywaitlist'] = $this->getWaitList($caseResumeList['data']);

        return $reportList;
    }

    /**
     * 待跟进人员信息列表
     * @param $dateFrom
     * @param $dateTo
     * @param int $rowFrom
     * @param int $rowTo
     * @return array
     * @throws \Exception
     */

    public function getWaitList($caseResumeList) {
        if (empty($caseResumeList)) {
            return [];
        }

        $result = [];
        $resumeIds = [];
        $caseIds = [];
        foreach ($caseResumeList as $item) {
            $resumeIds[] = $item['resumeid'];
            $caseIds[] = $item['caseid'];
            $result[] = [
                'caseid' => $item['caseid'],
                'resumeid' => $item['resumeid'],
                'flowat' => $item['flowat'],
                'flowatstatus' => $item['flowatstatus'],
                'timecontrol' => $item['timecontrol'],
                'time' => date("H:i", $item['timecontrol'])
            ];
        }

        $managerModel = new Manager();
        $info = $managerModel->getManagerListByResumeId($resumeIds);
        if (empty($info)) {
            return [];
        }


        $caseModel = new \App\Web\Model\CaseModel();
        $caseInfo = $caseModel->getCaseListWithCustomerInfo($caseIds);
        if (empty($caseInfo)) {
            return [];
        }

        $caseFlowModel = new CaseFlow();
        $flowList = $caseFlowModel->getDefaultFlow();
        foreach ($result as &$item) {
            foreach ($info as $infoItem) {
                if ($infoItem['resumeid'] == $item['resumeid']) {
                    $item['name'] = $infoItem['cname'];
                    $item['token'] = $infoItem['token'];
                }
            }
            foreach ($caseInfo as $caseItem) {
                if ($caseItem['caseid'] == $item['caseid']) {
                    $item['subname'] = $caseItem['subname'];
                    $item['projectname'] = $caseItem['projectname'];
                    $item['projectid'] = $caseItem['projectid'];
                    $item['subid'] = $caseItem['subid'];
                    $item['casename'] = $caseItem['casename'];
                }
            }
            $item['taskname'] = $flowList[$item['flowat']]['child'][$item['flowatstatus']]['taskname'];
            $item['desc'] = $flowList[$item['flowat']]['child'][$item['flowatstatus']]['desc'];
            $item['flowatname'] = $flowList[$item['flowat']]['name'];
            $item['flowatstatusname'] = $flowList[$item['flowat']]['child'][$item['flowatstatus']]['name'];
        }

        return $result;

    }
}